#!/bin/sh
# Copyright(c) 2012 OpenDomo Services SL. Licensed under GPL v3 or later

if test -z "$arch"; then
	arch="i486"
	export arch
fi

ACTUAL=`pwd`
cd ../../buildroot/output/host
TOOLCHAIN=`pwd`
cd $ACTUAL
CC=$TOOLCHAIN/usr/bin/$arch-linux-gcc

VERSION=3.1.3
REMOTE=http://www.cleancode.org/downloads/email
FILE=email-$VERSION.tar.gz
DIR=email-$VERSION

#ACTUAL="$ACTUAL/opendomo/opendomo-distro/external"
PACKAGES=$ACTUAL/pkg
TMPDIR=$ACTUAL
PKGNAME=odalerts

mkdir -p $PACKAGES
rm $PACKAGES/*.gz

# Configure/make
CC_PATH=$TOOLCHAIN/usr/bin/
export LDFLAGS="-L$LIBS_PATH -L$TMPDIR/usr/lib"
export CFLAGS="-I$INCL_PATH -I$TMPDIR/usr/include"
export CC="$CC"

if ! test -f $CC; then
	echo "ERROR: $CC is missing"
	exit 1
fi

# Configure/make
export PATH="$PATH:$CC_PATH"


#----------------------email---------------------------------

echo " ==== DOWNLOADING EMAIL ===="
# Download
mkdir -p downloads
if test -e downloads/$FILE
then
	echo "using previous download"
else
	cd downloads
	wget $REMOTE/$FILE
	cd ..
fi

# Uncompress
cd downloads
tar -zxvf $FILE
cd ..

export ac_cv_func_malloc_0_nonnull=yes
cd $ACTUAL/downloads/$DIR/dlib
./configure --host=$arch --prefix=$TMPDIR/usr
make

cd $ACTUAL/downloads/$DIR
./configure --host=$arch --prefix=$TMPDIR/usr
make || exit 1
make install || exit 1
cd $ACTUAL

#----------------------/email---------------------------------

#--------------------Packing----------------------------------
pwd

rm -fr usr/share/man usr/share/doc usr/etc
tar cvfz $PACKAGES/$PKGNAME-`date '+%Y%m%d'`.od.$arch.tar.gz usr var --owner 1000 --group 1000 --exclude "*~" --exclude .svn --exclude "*.a"
echo "" > $PACKAGES/$PKGNAME.deps
echo "OpenDomo Alerts" > $PACKAGES/$PKGNAME.desc
cd ..
